//
//  HMHomeTableViewCell.swift
//  Weibo-swift
//
//  Created by GD on 16/9/23.
//  Copyright © 2016年 geduo. All rights reserved.
//

import UIKit
/*
 1.原创微博视图
 
 2.转发微博视图
 
 3.底部toolBar视图
 */
let HMHomeTableViewCellMargin : CGFloat = 10

//屏幕的宽度
let ScreenWidth = UIScreen.main.bounds.size.width

let ScreenHeith = UIScreen.main.bounds.size.height


class HMHomeTableViewCell: UITableViewCell {
    
    //  记录当前toolbar顶部约束
    var toolBarTopConstraint: Constraint?

    
    //持有 ViewModel
    var  statusViewModel : HMStatusViewModel?{
        didSet{//类似 MVC中的重新模型set方法
            //可以给cell 里面响应的子控件绑定数据
            originalView.statusViewModel = statusViewModel // 把model传给了其子视图.
            toolBar.statusViewModel = statusViewModel
            
            
            //  把toolbar顶部约束卸载掉
            toolBarTopConstraint?.uninstall()
            
            //  判断其是否有转发微博  其实就是更新了toolBar的顶部约束,根据上面转发视图的有无,即约束参考的有无.来更新toolBar的顶部约束. 
            if statusViewModel?.status?.retweeted_status != nil {
                //  有转发微博
                //  更新约束,显示转发微博,绑定数据
                retweetView.isHidden = false
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                retweetView.statusViewModel = statusViewModel
            }
            else {
                //  没有转发微博
                //  更新约束, 隐藏转发微博视图
                retweetView.isHidden = true
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            print(statusViewModel?.status?.pic_urls?.first?.thumbnail_pic)
        }
        
        
    }
    
     //  MARK: --    懒加载控件
    private lazy var originalView:HMStatusOriginalView = {
        let view = HMStatusOriginalView()
        
//        view.backgroundColor = RandomColor()
        return view
    
    }()
    
    //  转发微博视图
    private lazy var retweetView: HMStatusRetweetView = {
        let view = HMStatusRetweetView()
//        view.backgroundColor = RandomColor()
        return view
    }()

    
    //  toolbar视图
    private lazy var toolBar: HMStatusToolBar = HMStatusToolBar()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(self.contentView)
            make.left.right.equalTo(self.contentView)
//            make.height.equalTo(100)
            //底部约束, 在originView 内部设置,关键约束
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
        }

        
        toolBar.snp_makeConstraints { (make) in
//            toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
           toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
            make.height.equalTo(35)
            
            //  关键约束 -> toolbar的底部约束等于contentView的底部约束
            make.bottom.equalTo(contentView.snp_bottom)
        }

    }
    
    
    
    
    
    
    
    
    
    
    
    
    
   
}
